﻿using System;

namespace YK.Infrastructure.NH.Mapping
{
    using NHibernate.Mapping.ByCode;
    using YK.Domain.Models;

    public class UserMapping : MappingBase<User>
    {
        public UserMapping()
            : base("DA_USER", "DU_ID")
        {
            Property(m => m.Pass, map => map.Column("DU_PASS"));
            Property(m => m.Name, map => 
                { 
                    map.Column("DU_NAME");
                    map.Length(100);
                    map.NotNullable(true);
                });

            Bag(m => m.Roles, map =>
            {
                map.Table("DA_USER_ROLE");
                map.Key(keyMapper => keyMapper.Column("DUR_USER_ID"));
                map.Cascade(Cascade.All);  //设置cascade属性为all，用于从一端保存数据。
            }, rel => rel.ManyToMany(m => m.Column("DUR_ROLE_ID"))); //DUR_ROLE_ID
        }
    }

    public class RoleMapping : MappingBase<Role>
    {
        public RoleMapping()
            : base("DA_ROLE", "DR_ID")
        {
            Property(m => m.Name, map => map.Column("DR_RNAME"));
            Property(m => m.Desc, map => map.Column("DR_RDESC"));

            Bag(m => m.Users, map =>
            {
                map.Table("DA_USER_ROLE");
                map.Key(keyMapper => keyMapper.Column("DUR_ROLE_ID"));
                map.Cascade(Cascade.All);  //设置cascade属性为all，用于从一端保存数据。
            }, rel => rel.ManyToMany(m => m.Column("DUR_USER_ID")));  //DUR_USER_ID
        }
    }

}
